﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using MvcAppFinal1.Models.OMS.Model.Repositories;
using MvcAppFinal1.Models.OMS.Domain.Model;

namespace MvcAppFinal1.Models.OMS.Infrastracture.Repositories
{
    public class UserRepository : DbContext, IUserRepository
    {
        public DbSet<User> Users { get; set; }
        // Add
        public User Add(User user)
        {
            if (user != null)
            {
                User us1 = Users.Add(user);

                if (us1 == user)
                {
                    return null;
                }
                else return user;
            }
            else throw new ArgumentNullException();
        }
        // Delete
        public User Delete(User user)
        {
            if (user != null)
            {
                try
                {
                    User us1 = Users.Remove(user);
                    if (us1 == user)
                    {
                        return null;
                    }
                    else return user;
                }
                catch (InvalidOperationException )
                {
                    return user;
                }
            }
            else throw new ArgumentNullException();
        }
        // Update
        public bool Update(User user)
        {
            if (user == null)
            {
                throw new ArgumentNullException();
            }
            User updUser = GetById(user.UserId);
            if (updUser == null)
            {
                return false;
            }
            Users.Remove(updUser);
            Users.Add(user);
            return true;
        }
        // List
        public IList<User> UserToList()
        {
            return Users.ToList();
        }
        // Query
        public IQueryable<User> UserToQuery()
        {
            return Users.AsQueryable();
        }
        // Get by Id
        public User GetById(int id)
        {
            User us1 = Users.Select(u => u).Where(u => u.UserId == id).FirstOrDefault();
            return us1;
        }
        // Get by Login
        public User GetByLogin(string login)
        {
            User us1 = Users.Select(u => u).Where(u => u.LoginName == login).FirstOrDefault();
            return us1;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
        /////////////////
        //public List<User> Users = new List<User>();

        //public User Add(User user)
        //{
        //    if (user != null)
        //    {
        //        Users.Add(user);
        //    }
        //    return null;
        //}

        //public User GetById(int id)
        //{
        //    User us1 = Users.Select(u => u).Where(u => u.UserId == id).FirstOrDefault();
        //    return us1;
        //}
        ///////////////
        //protected override void OnModelCreating(DbModelBuilder modelBuilder)
        //{
        //    //modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
        //    modelBuilder.Entity<User>().ToTable("User");
        //    base.OnModelCreating(modelBuilder);
        //}

        //private DbSet<User> contextUser;
        //public UserRepository() { }

        //public UserRepository(DbContext contextU)
        //{
        //    contextUser = contextU;
        //}
        //private IDbSet<User> dbSetU
        //{
        //    get
        //    {
        //        return contextUser.Set<User>();
        //    }
        //}

        //public User Add(User user)
        //{
        //    User us1 = dbSetU.Add(user);
        //    if (us1 == user)
        //    {
        //        return null;
        //    }
        //    else return user;
        //}
        //public User GetById(int id)
        //{
        //    User us1 = dbSetU.Select(u => u).Where(u => u.UserId == id).FirstOrDefault();
        //    return us1;
        //}
    }
}